package com.heatingcharge.service.charge.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heatingcharge.common.util.ChargeUtil;
import com.heatingcharge.dto.ChargePayableDTO;
import com.heatingcharge.dto.ResHouseDTO;
import com.heatingcharge.entity.base.ChargePrice;
import com.heatingcharge.entity.charge.ChargePayable;
import com.heatingcharge.entity.unit.ResHouse;
import com.heatingcharge.mapper.base.ChargePriceMapper;
import com.heatingcharge.mapper.charge.ChargePayableMapper;
import com.heatingcharge.mapper.customer.CustomerMapper;
import com.heatingcharge.mapper.unit.ResHouseMapper;
import com.heatingcharge.param.SearchParam;
import com.heatingcharge.security.JwtUser;
import com.heatingcharge.security.util.JwtUtil;
import com.heatingcharge.security.util.SecurityUtil;
import com.heatingcharge.service.base.IChargePriceService;
import com.heatingcharge.service.charge.IChargePayableService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heatingcharge.service.unit.IResHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 年度应收 服务实现类
 * </p>
 *
 * @author devin.jiang
 * @since 2019-10-31
 */
@Service
public class ChargePayableServiceImpl extends ServiceImpl<ChargePayableMapper, ChargePayable> implements IChargePayableService {
    @Autowired
    IResHouseService resHouseService;
    @Autowired
    IChargePriceService chargePriceService;

    @Override
    public List<ChargePayableDTO> getChargePayable(SearchParam searchParam) {
        return baseMapper.selectChargePayable(searchParam);
    }

    @Override
    public List<ChargePayableDTO> getArreasInfo(SearchParam searchParam) {
        return baseMapper.selectArreasInfo(searchParam);
    }

    @Override
    public Integer save(SearchParam searchParam) {
        //获取所有的房间信息
        List<ResHouseDTO> resHouses = resHouseService.getHouses(searchParam);
        //获取指定供暖季的价格信息
        ChargePrice chargePrice = chargePriceService.getChargePriceByHeatYearId(searchParam.heatYearId);
        //年度应缴
        ChargePayable chargePayable;
        //缴费单价
        BigDecimal chargeUnitPrice;
        //房间类型
        Integer houseType;
        //缴费面积
        BigDecimal chargeArea;
        //面积应缴
        BigDecimal areaPayable;
        Integer result=0;

        JwtUser user = SecurityUtil.getUser();
        Integer extistId=0;
        if (resHouses.size()>0){
            for (int i = 0; i < resHouses.size(); i++) {
                houseType=resHouses.get(i).getHouseType();
                chargeUnitPrice= ChargeUtil.getChargeUnitPrice(chargePrice,houseType);
                chargeArea=resHouses.get(i).getChargeArea();
                areaPayable=chargeArea.multiply(chargeUnitPrice);
                chargePayable=ChargePayable.builder()
                        .heatYearId(searchParam.getHeatYearId())
                        .houseId(resHouses.get(i).getId())
                        .buildArea(resHouses.get(i).getBuildArea())
                        .useArea(resHouses.get(i).getUseArea())
                        .heatArea(resHouses.get(i).getHeatArea())
                        .chargeArea(resHouses.get(i).getChargeArea())
                        .floorHeight(resHouses.get(i).getFloorHeight())
                        .floorFactor(resHouses.get(i).getFloorFactor())
                        .houseType(houseType)
                        .chargeType(resHouses.get(i).getChargeType())
                        .heatStatus(resHouses.get(i).getHeatStatus())
                        .chargePrice(chargeUnitPrice)
                        .areaPayable(areaPayable)
                        .discount(new BigDecimal(0))
                        .additional(new BigDecimal(0))
                        .reduction(new BigDecimal(0))
                        .subsidy(new BigDecimal(0))
                        .carryForward(new BigDecimal(0))//TODO 结转金额
                        .actualPayable(areaPayable)//TODO 实际应缴金额
                        .subsidy(new BigDecimal(0))
                        .chargeState(0)
                        .build();
                extistId=checkExist(searchParam.getHeatYearId(),resHouses.get(i).getId());
                if (extistId>0){ //如果存在  则更新记录
                    chargePayable.setId(extistId);
                    chargePayable.setUpdatedBy(user.getRealName());
                    result+= baseMapper.updateById(chargePayable);
                }
                else { //如果不存在  则插入记录
                    chargePayable.setCreatedBy(user.getRealName());
                    result+= baseMapper.insert(chargePayable);
                }
            }
        }
        return result;
    }

    @Override
    public Integer update(ChargePayable chargePayable) {
//        QueryWrapper<ChargePayable> queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("id",chargePayable.getId());
//        queryWrapper.eq("del_flag",0);
//        ChargePayable oldChargePayable = baseMapper.selectOne(queryWrapper);
//        if (ObjectUtil.isNotNull(oldChargePayable)){ //如果存在  则更新记录
//            Integer chargeState=2;//欠费
//            //附加费用
//            oldChargePayable.setAdditional(chargePayable.getAdditional());
//            //减免金额
//            oldChargePayable.setReduction(chargePayable.getReduction());
//            //补贴金额
//            oldChargePayable.setSubsidy(chargePayable.getSubsidy());
//            //优惠金额
//            oldChargePayable.setDiscount(chargePayable.getDiscount());
//            //实际应缴
//            oldChargePayable.setActualPayable(chargePayable.getActualPayable());
//            //缴费状态 欠费
//            chargePayable.setChargeState(chargeState);
//            return baseMapper.updateById(oldChargePayable);
//        }
//        return 0;
        return baseMapper.updateById(chargePayable);
    }

    /**
     * 检查是否存在指定供暖季和房间的应缴信息  如果存在 返回主键ID，如果不存在 返回0
     * @param heatYearId
     * @param houseId
     * @return
     */
    private Integer checkExist(Integer heatYearId,Integer houseId){
        QueryWrapper<ChargePayable> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("heat_year_id",heatYearId);
        queryWrapper.eq("house_id",houseId);
        queryWrapper.eq("del_flag",0);
        ChargePayable chargePayable = baseMapper.selectOne(queryWrapper);
        if (ObjectUtil.isNotNull(chargePayable)){
            return chargePayable.getId();
        }
        return 0;
    }
}
